package com.czl.module_user.fragment

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.annimon.stream.Collectors
import com.annimon.stream.Stream
import com.czl.base.base.BaseFragment
import com.czl.base.config.AppConstants
import com.czl.base.event.LiveBusCenter
import com.czl.base.util.DialogHelper
import com.czl.module_user.BR
import com.czl.module_user.R
import com.czl.module_user.adapter.PeopleManagerAdapter
import com.czl.module_user.databinding.FragmentPeopleManagerBinding
import com.czl.module_user.viewmodel.PeopleManagerViewModel
import java.util.ArrayList

/**
 *
 * @Description:人员管理
 * @Author: XCH
 * @CreateDate: 2022/1/6 15:26
 */
@Route(path = AppConstants.Router.User.F_PEOPLE_MANAGER)
class PeopleManagerFragment : BaseFragment<FragmentPeopleManagerBinding, PeopleManagerViewModel>() {

    private lateinit var mAdapter: PeopleManagerAdapter

    override fun initContentView(): Int {
        return R.layout.fragment_people_manager
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }

    override fun initData() {
        viewModel.tvTitle.set("人员管理")
        initAdapter()
        reload()
    }

    private fun initAdapter() {
        mAdapter = PeopleManagerAdapter(this)
        binding.ryCommon.apply {
            layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
            adapter = mAdapter
        }
    }

    override fun initViewObservable() {
        viewModel.uc.loadCompleteEvent.observe(this, { data ->
            handleRecyclerviewData(
                data == null,
                data as MutableList<*>?,
                mAdapter,
                binding.ryCommon,
                binding.smartCommon,
                1,
                true
            )
        })

        viewModel.uc.refreshListEvent.observe(this, {
            binding.smartCommon.autoRefresh()
        })

        LiveBusCenter.observeRefreshPeopleEvent(this, {
            binding.smartCommon.autoRefresh()
        })

        viewModel.uc.choiceHouseEvent.observe(this, {
            DialogHelper.showBottomListDialog(
                requireContext(),
                Stream.of(it).map { e -> e.houseCode }.withoutNulls()
                    .collect(Collectors.toList()) as ArrayList<String>
            ) { position, text ->
                viewModel.houseName.set(text)
                viewModel.houseId = it[position].houseId
                binding.smartCommon.autoRefresh()
            }
        })

        viewModel.uc.deleteEvent.observe(this, {
            DialogHelper.showBaseDialog(requireContext(), "", "确定删除该人员吗？") {
                viewModel.deleteHouseUser(it)
            }
        })

        viewModel.uc.renewEvent.observe(this, {
            startContainerActivity(AppConstants.Router.User.F_PEOPLE_DETAIL, Bundle().apply {
                putString(AppConstants.BundleKey.PEOPLE_ID, it.id)
                putString(AppConstants.BundleKey.OWNER_ID, it.ownerId)
                putInt(AppConstants.BundleKey.PEOPLE_FROM, 0)
            })
        })

        viewModel.uc.deleteSuccessEvent.observe(this, {
            binding.smartCommon.autoRefresh()
        })

    }

    override fun reload() {
        super.reload()
        viewModel.getUserRooms()
    }

}